{extend name="layout/base"}
{block name="title"}管理员列表{/block}
{block name="main"}
<div class="right-main">
    <div class="mdui-card border-1 mdui-shadow-0 mdui-m-b-2 ">
        <div class="mdui-card-primary">
            <div class="mdui-toolbar mdui-p-a-0">
                <a href="#">
                    <button class="mdui-btn border-1"><a href="/admin/user/create">添加用户</a></button>
                </a>
                <div class="mdui-toolbar-spacer"></div>
                <div class="mdui-textfield">
                    <i class="mdui-icon material-icons">search</i>
                    <input id="keyword" class="mdui-textfield-input" type="email" placeholder="search"/>
                </div>
                <button class="mdui-btn border-1"
                        onclick="location.href = '/admin/user?username=' + document.getElementById('keyword').value">搜索
                </button>
                <button class="mdui-btn border-1" onclick="location.href = '/admin/user'">刷新</button>
            </div>
        </div>
        <div class="mdui-divider "></div>
        <div class="mdui-table-fluid mdui-m-t-1 mdui-m-a-1 mdui-shadow-0 layui-form">
            <table class="mdui-table mdui-table-hoverable">
                <thead>
                <tr>
                    <th>#</th>
                    <th>用户名</th>
                    <th>超级管理员</th>
                    <th>状态</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {foreach $admins as $key=>$vo }
                <tr>
                    <td>{$vo.id}</td>
                    <td>{$vo.username}</td>
                    <td>{if $vo.super eq 1} 是 {else /} 否 {/if}</td>
                    <td>
                        <input type="checkbox" data-id="{$vo.id}" lay-filter="status" lay-text="正常|封禁" name="switch"
                               {if $vo.status eq 1/} checked value="1" {else/} value="0" {/if}
                        {if session('admin.super') neq true} disabled {/if}
                        lay-skin="switch">

                    </td>
                    <td>{$vo.create_time}</td>
                    <td>
                        {if session('admin.super') eq true}
                        <a href="/admin/user/{$vo.id}/edit" class="link"><i
                                class="mdui-icon material-icons">&#xe3c9;</i></a>
                        |
                        <a href="javascript:;" onclick="deleteAdmin({$vo.id})"><i class="mdui-icon material-icons">&#xe872;</i></a>
                        {else/}
                        无权限
                        {/if}
                    </td>
                </tr>
                {/foreach}

                </tbody>
            </table>
        </div>
    </div>
</div>


<div class="mws-panel-content">
    <div class="list-page">
        {$admins|raw}
    </div>
</div>

{/block}

{block name="script"}

<script>
    function deleteAdmin(uid) {
        layer.alert('确认删除？', function (index) {
            $.post('/admin/user/' + uid, {_method: 'DELETE'}, res => {
                if (res.code === 1) {
                    layer.msg(res.msg, {icon: 6, time: 1500}, () => {
                        location.href = res.url
                    })
                } else {
                    layer.msg(res.msg, {icon: 5, time: 1500}, () => {
                        location.href = res.url
                    })
                }
            })
            layer.close(index);
        });
        return false;
    }

    layui.use('form', function () {
        let form = layui.form, layer = layui.layer, $ = layui.jquery;
        form.on('switch(status)', function (data) {
            let id = $(data.elem).attr("data-id");
            let value = $(data.elem).attr("value") === '1' ? 0 : 1;
            $.post('/admin/user/up_state/' + id, {status: value}, res => {
                if (res.code === 1) layer.msg(res.msg, {icon: 6, time: 900, anim: 2})
                else layer.msg(res.msg, {icon: 5, time: 1000, anim: 6})
            })
        });
    });
</script>
{/block}